package com.piece.core.log.client;

import com.piece.core.log.properties.LogProperties;
import lombok.Getter;
import org.springframework.context.ApplicationContext;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 日志工厂
 */
@Getter
public class LogFactory {

    private ApplicationContext applicationContext;
    private LogProperties logProperties;
    private Map<String, AuditLogService> auditLogServiceMap = new HashMap<>();
    private Map<String, BeanPostLogService> beanPostLogServiceMap = new HashMap<>();
    private Map<String, LoginLogService> loginLogServiceMap = new HashMap<>();
    private Map<String, MessageLogService> messageLogServiceMap = new HashMap<>();

    public LogFactory(ApplicationContext applicationContext, LogProperties logProperties) {
        this.logProperties = logProperties;
        this.applicationContext = applicationContext;
    }

    @PostConstruct
    public void init() {
        Map<String, AuditLogService> interfaces_1 = applicationContext.getBeansOfType(AuditLogService.class);
        Iterator<Map.Entry<String, AuditLogService>> it_1 = interfaces_1.entrySet().iterator();
        while (it_1.hasNext()) {
            Map.Entry<String, AuditLogService> entry = it_1.next();
            auditLogServiceMap.put(entry.getKey(), entry.getValue());
        }

        Map<String, BeanPostLogService> interfaces_2 = applicationContext.getBeansOfType(BeanPostLogService.class);
        Iterator<Map.Entry<String, BeanPostLogService>> it_2 = interfaces_2.entrySet().iterator();
        while (it_2.hasNext()) {
            Map.Entry<String, BeanPostLogService> entry = it_2.next();
            beanPostLogServiceMap.put(entry.getKey(), entry.getValue());
        }

        Map<String, LoginLogService> interfaces_3 = applicationContext.getBeansOfType(LoginLogService.class);
        Iterator<Map.Entry<String, LoginLogService>> it_3 = interfaces_3.entrySet().iterator();
        while (it_3.hasNext()) {
            Map.Entry<String, LoginLogService> entry = it_3.next();
            loginLogServiceMap.put(entry.getKey(), entry.getValue());
        }

        Map<String, MessageLogService> interfaces_4 = applicationContext.getBeansOfType(MessageLogService.class);
        Iterator<Map.Entry<String, MessageLogService>> it_4 = interfaces_4.entrySet().iterator();
        while (it_3.hasNext()) {
            Map.Entry<String, MessageLogService> entry = it_4.next();
            messageLogServiceMap.put(entry.getKey(), entry.getValue());
        }
    }

    public AuditLogService getAuditLog() {
        return auditLogServiceMap.get("audit_log_service_" + getLogType());
    }

    public BeanPostLogService getBeanPostLog() {
        return beanPostLogServiceMap.get("bean_post_log_service_" + getLogType());
    }

    public LoginLogService getLoginLog() {
        return loginLogServiceMap.get("login_log_service_" + getLogType());
    }

    public MessageLogService getMessageLog() {
        return messageLogServiceMap.get("message_log_service_" + getLogType());
    }

    private String getLogType() {
        String scheme = logProperties.getType();
        if (StringUtils.isEmpty(scheme)) {
            scheme = "logger";
        }

        return scheme;
    }
}